package br.com.megaacerto.security;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.megaacerto.dto.UsuarioLogadoDTO;

public class FilterSecurity implements Filter  {

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {

		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		UsuarioLogadoDTO usuarioDTO = (UsuarioLogadoDTO) httpServletRequest.getSession().getAttribute("usuario");
		if(usuarioDTO != null && (usuarioDTO.isLogadoUser() || usuarioDTO.isLogadoAdmin())){
			filterChain.doFilter(request, response);
		}else{
			HttpServletResponse httpServletResponse = (HttpServletResponse) response;
			httpServletResponse.sendRedirect("http://localhost:8080/megaacerto/page/login.html");
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}
	
	

}
